import time
from logging import DEBUG

from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.chrome.webdriver import WebDriver
from ...constants.common_sequence import SWITCH_ENV
from ...exception.customer_exception import CustomerException

from ...models.domain.vip_search_detail import VipSearchDetail
import logging
from ...constants.common_sequence import VipWay
from ...models.enums.app_http_code_enum import AppHttpCodeEnum
from ...page.pay_page.pay_out_page import PayOutPage

logger = logging.getLogger(__name__)
"""
Description
    会员的搜索页
"""


class VipSearchPage(object):

    def __init__(self, cilent):
        self.client = cilent
        self.driver: WebDriver = cilent.driver

    """
    功能逻辑
    """

    def _search_vip_email(self, search_text):
        """
        邮箱搜索
        """
        self.driver.find_element(AppiumBy.ID, f"{SWITCH_ENV}:id/tv_email").click()
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/et_search').send_keys(search_text)
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_search').click()
        time.sleep(1)
        user_element = self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/recycler_view_user')
        user_element.find_element(AppiumBy.XPATH,
                                  f'//*[contains(@resource-id,"{SWITCH_ENV}:id/cb_select")]').click()

    def _search_vip_telephone(self, search_text):
        """
        电话搜索
        """
        self.driver.find_element(AppiumBy.ID, f"{SWITCH_ENV}:id/ll_phone_title").click()
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/et_search').send_keys(search_text)
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_search').click()
        time.sleep(1)
        user_element = self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/recycler_view_user')
        user_element.find_element(AppiumBy.XPATH,
                                  f'//*[contains(@resource-id,"{SWITCH_ENV}:id/cb_select")]').click()

    def _search_vip_id(self, search_text):
        """
        ID搜索
        """
        self.driver.find_element(AppiumBy.ID, f"{SWITCH_ENV}:id/ll_user_id_title").click()
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/et_search').send_keys(search_text)
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_search').click()
        time.sleep(1)
        user_element = self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/recycler_view_user')
        user_element.find_element(AppiumBy.XPATH,
                                  f'//*[contains(@resource-id,"{SWITCH_ENV}:id/cb_select")]').click()

    def _confirm(self):
        """
        确认
        """
        self.driver.find_element(AppiumBy.ID, f"{SWITCH_ENV}:id/tv_confirm").click()

    def _back(self):
        """
        退出
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/ll_cancel').click()

    """
    按钮方法
    """

    def search_vip(self, vip_search_detail: VipSearchDetail):
        """
        Desc:搜索Vip
        Params:
            VipSearchDetail:会员搜索
        """
        try:
            vip_way = vip_search_detail.vip_way
            search_text = vip_search_detail.search_text
            if vip_way == VipWay.telephone.value:
                self._search_vip_telephone(search_text)
            elif vip_way == VipWay.email.value:
                self._search_vip_email(search_text)
            elif vip_way == VipWay.id.value:
                self._search_vip_id(search_text)
            self._confirm()
        except Exception as e:
            self._back()
            raise CustomerException(AppHttpCodeEnum.SEARCH_VIP_FILE)
        return PayOutPage(self.client)
